//package com.spider.im.chat.server;
//
//import com.spider.im.chat.proto.ChatProto;
//import io.netty.bootstrap.ServerBootstrap;
//import io.netty.channel.ChannelFuture;
//import io.netty.channel.ChannelInitializer;
//import io.netty.channel.ChannelOption;
//import io.netty.channel.EventLoopGroup;
//import io.netty.channel.nio.NioEventLoopGroup;
//import io.netty.channel.socket.SocketChannel;
//import io.netty.channel.socket.nio.NioServerSocketChannel;
//import io.netty.handler.codec.http.HttpObjectAggregator;
//import io.netty.handler.codec.http.HttpServerCodec;
//import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
//import io.netty.handler.codec.protobuf.ProtobufDecoder;
//import io.netty.handler.codec.protobuf.ProtobufEncoder;
//import io.netty.handler.codec.protobuf.ProtobufVarint32FrameDecoder;
//import io.netty.handler.codec.protobuf.ProtobufVarint32LengthFieldPrepender;
//import lombok.extern.slf4j.Slf4j;
//import org.springframework.beans.factory.annotation.Value;
//import org.springframework.stereotype.Component;
//
//import java.util.concurrent.atomic.AtomicInteger;
//
//@Slf4j
//@Component
//public class IMServer {
//    @Value("${netty.port:8888}")
//    private int port;
//    public final static String WEB_SOCKET_PATH = "/websocket";
//    private final AtomicInteger connectionsCount = new AtomicInteger(0);
////    @PostConstruct
//    public void run() throws Exception {
//        HandlerRegistry registry = new HandlerRegistry();
//        AnnotationModuleLoader.loadHandlers("com.spider.im.chat.modules", registry);
//        EventLoopGroup bossGroup = new NioEventLoopGroup();
//        //EventLoopGroup bg = new EpollEventLoopGroup();//NioEventLoopGroup
//        EventLoopGroup workerGroup = new NioEventLoopGroup();
//        try{
//            ServerBootstrap b = new ServerBootstrap();
//            b.group(bossGroup,workerGroup)
//                    .channel(NioServerSocketChannel.class)
//                    .childHandler(new ChannelInitializer<SocketChannel>() {
//                        @Override
//                        protected void initChannel(SocketChannel socketChannel) throws Exception {
//                            socketChannel.pipeline()
//                                    .addLast(new ProtobufVarint32FrameDecoder())
//                                    .addLast(new ProtobufDecoder(ChatProto.MessageWrapper.getDefaultInstance()))
//                                    .addLast(new ProtobufVarint32LengthFieldPrepender())
//                                    .addLast(new ProtobufEncoder())
//                                    .addLast(new HttpServerCodec() ,// HTTP编解码器
//                                    new HttpObjectAggregator(65536), // HTTP消息聚合器
//                                    new WebSocketServerProtocolHandler("/ws")) // WebSocket协议处理器
//
//                                    // 添加 /websocket路由用于识别websocket请求, 方便在nginx中作localtion配置
//                                    .addLast(new WebSocketServerProtocolHandler(WEB_SOCKET_PATH))
//                                    .addLast(new IMServerHandler(registry, connectionsCount));
//                        }
//                    })
//                    .option(ChannelOption.SO_BACKLOG,128)
//                    .childOption(ChannelOption.SO_KEEPALIVE, true);
//
//            ChannelFuture f = b.bind(port).sync();
//            log.info("Netty Server started on port {}" ,port);
//            f.channel().closeFuture().sync();
//        }
//        finally {
//            workerGroup.shutdownGracefully();
//            bossGroup.shutdownGracefully();
//        }
//
//    }
//
//}
